package com.fc.backingbeans.journal;


import javax.faces.bean.ViewScoped
import javax.faces.event.ActionEvent

import org.primefaces.event.ScheduleEntryMoveEvent
import org.primefaces.event.SelectEvent
import org.primefaces.model.DefaultScheduleEvent
import org.primefaces.model.DefaultScheduleModel
import org.primefaces.model.ScheduleEvent
import org.primefaces.model.ScheduleModel
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

import com.fc.entities.Activity
import com.fc.entities.User
import com.fc.entities.Workout
import com.fc.entities.WorkoutJournal
import com.fc.service.UserDetailsService
import com.fc.service.UserService
import com.fc.service.WorkoutJournalService
import com.fc.service.WorkoutService

@Component("workoutJournalController")
@ViewScoped
class WorkoutJournalController {
	ScheduleModel eventModel
	ScheduleEvent event = new DefaultScheduleEvent()
	WorkoutJournal entry = new WorkoutJournal()
	WorkoutJournal readonlyEntry
	Activity selectedActivity
	@Autowired private WorkoutJournalService service
	@Autowired private UserDetailsService userDetailsService
	@Autowired private UserService userService
	@Autowired private WorkoutService workoutService

	Activity getSelectedActivity() {
		return selectedActivity
	}

	void setSelectedActivity(Activity selectedActivity) {
		this.selectedActivity = selectedActivity
	}
	ScheduleModel getEventModel() {
		if (eventModel == null) {
			eventModel = new DefaultScheduleModel()
			service.findAllByUser(userDetailsService.currentUser)?.each { WorkoutJournal entry ->
				eventModel.addEvent(new DefaultScheduleEvent(entry.workout.name, entry.date, entry.date, entry))
			}
		}

		return eventModel
	}

	WorkoutJournal getReadonlyEntry() {
		return readonlyEntry
	}
	void create(WorkoutJournal entry) {
		if (entry != null) {
			entry.user = userDetailsService.currentUser
			service.create(entry)
			entry = new WorkoutJournal()
		}
	}

	void delete(WorkoutJournal entry) {
		if (entry != null) {
			service.delete(entry)
			entry = new WorkoutJournal()
		}
	}

	void addEvent(ActionEvent actionEvent) {
		if(event.getId() == null) {
			event = new DefaultScheduleEvent(entry.workout.name, entry.date, entry.date, entry)
			create(entry)
			eventModel.addEvent(event)
		}
		else {
			eventModel.updateEvent(event)
		}

		entry = new WorkoutJournal()
		event = new DefaultScheduleEvent()
	}

	void onEventSelect(SelectEvent selectEvent) {
		event = (ScheduleEvent) selectEvent.getObject();
		readonlyEntry = (WorkoutJournal) event.getData()
		readonlyEntry = service.find(WorkoutJournal.class, readonlyEntry.id)
	}

	void onDateSelect(SelectEvent selectEvent) {
		Date date = (Date) selectEvent.getObject()
		event = new DefaultScheduleEvent("", date, date, false)
		entry.setDate((Date) event.getStartDate())
	}

	void onEventMove(ScheduleEntryMoveEvent event) {
		ScheduleEvent sch = event.getScheduleEvent();
		WorkoutJournal entry = sch.data;
		Date newStartDate = sch.startDate;
		service.save(entry);
		entry = new WorkoutJournal()
	}

	/**
	 * Visszaadja a felhasználó által felvett és az alapértelmezett edzéseket
	 * @return
	 */
	List<Workout> findWorkouts() {
		List<Workout> workouts = []
		workouts += workoutService.findAllByUser(userDetailsService.currentUser)
		workouts += workoutService.findAllByUser(userService.find(User.class, 1))
		return workouts
	}
}
